"use strict";
var PlayerManager = function(url,canvas) {
    this.url = url;
    this.canvas = canvas;
    this.videoMode = kVideoVodMode;
    this.player = null;
    this.logger = null;


    this.initPlayer();
    this.initLogger();

    this.onTimeupdate = null;
    this.onLoadstart = null;
    this.onPlaying = null;
    this.onPause = null;
    this.onStop = null;
    this.onReadyToPlay = null;
    this.onLiveFinish = null;
};

PlayerManager.prototype = {

    initPlayer: function() {
        if (!self.singlePlayer) {
            self.singlePlayer = new Player();
        }
        this.player = self.singlePlayer;

        this.player.onTimeupdate = (currrentT,durationT) => {
            if (this.onTimeupdate) {
                this.onTimeupdate(currrentT/1000,durationT/1000);
            }
        };
        this.player.onLoadstart = () => {
            if (this.onLoadstart) {
                this.onLoadstart();
            }
        };
        this.player.onPlaying = () => {
            if (this.onPlaying) {
                this.onPlaying();
            }
        };
        this.player.onPause = () => {
            if (this.onPause) {
                this.onPause();
            }
        };

        this.player.onStop = ()=> {
            if (this.onStop) {
                this.onStop();
            }
        };

        this.player.onReadyToPlay = (durationT)=> {
            this.replay = false;

            if (this.onReadyToPlay) {
                this.onReadyToPlay(durationT/1000);
            }
        };

        this.player.onLiveFinish = () => {
            if(this.onLiveFinish) {
                this.onLiveFinish();
            }
        }
    },

    initLogger: function() {
        this.logger = new Logger("Page");
    },

    live: function() {
        this.videoMode = kVideoLiveMode;
        this.playVideo();
    },
    play: function() {
        this.videoMode = kVideoVodMode;
        this.playVideo();
    },

    setVideoMode: function(mode,url) {
        this.stopVideo();
        this.videoMode = mode;
        this.url = url;
        this.playVideo();
    },

    playVideo: function() {
        if (this.videoMode != kVideoVodMode) {
            // 设置加速参数：缓存5秒启动加速，1.5秒停止加速，播放3帧丢2帧
            this.player.setAcceleration(500, 100, 3, 2);
        }

        this.player.play(this.url, this.canvas, function (e) {
            console.log("play error " + e.error + " status " + e.status + ".");
            this.replay = false;
            if (e.error == 1) {
                this.logger.logInfo("Finished.");
            }
        }.bind(this), ((this.videoMode == kVideoVodMode) ? 320 : 16) * 1024, this.videoMode);
    },

    replayVideo: function() {
        if(this.replay) return;

        this.player.stop();
        this.replay = true;
        setTimeout(() => {
            this.playVideo();
        }, 1000);
    },

    stopVideo: function() {
        if (!this.player) {
            return;
        }
        this.player.stop();
        this.player = null;
    },

    pause: function() {
        if (!this.player) {
            return;
        }
        this.player.pause();
    },

    setTrack: function(timeTrack, timeLabel,durationLabel) {
        if (!this.player) {
            return;
        }
        this.player.setTrack(timeTrack, timeLabel,durationLabel);
    },
    getState: function() {
        return this.player.getState();
    },

    seekTo: function(ms) {
        console.log("manager seekto ms"+ms);
        this.player.seekTo(ms);
    },

    getCurrentTime: function() {
        return this.player.currentPlayTime/1000;
    },

    getDurationTime: function() {
        return this.player.duration/1000;
    },
    
    audioContextUnSuspend: function() {
        this.player.audioContextUnSuspend();
    },
    
    audioPause: function() {
        this.player.audioPause();
    },
    
    isMute: function(muteCallback) {
        return this.player.isMute(muteCallback);
    }

};